<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\api\model;

use \think\Model;
class Classify extends Model
{

    public function classify(){

        $shu=[];
        $a=$this->where('classify_status','1')->field('classify_id as class_id,classify_name as class_name')->select();
        $tag=model('Tag')
            ->where('tag_type',1)
            ->where('tag_status',1)
            ->field('tag_id,tag_name')
            ->select();
        if ($a==true){

            $shu=[
                'code'=>0,
                'msg'=>'请求成功',
                'data'=>[
                    'classify'=>$a,
                    'tag'=>$tag
                ]
            ];
        }else{
            $shu=[
                'code'=>1,
                'msg'=>'请求失败',

            ];
        }




        return $shu;
    }

    public function classifypoi($t='',$lat2,$lng2){

        if ($t!=''){
            $id=$t;
            $data=[];
            $poi=model('Poi')
                ->where('typeid',$id)
                ->where('deleted',0)
                ->where('is_stop',0)
                ->field('id,name,eng_name,lat,lng,thumb,score')
//                ->order('score asc')
                ->select();


            foreach ($poi as $k=>$v){
                $data[$k]['poi_id']=$v['id'];
                $data[$k]['name']=$v['name'];
                $data[$k]['eng_name']=$v['eng_name'];
                $data[$k]['score']=$v['score'];

                //计算距离
                $juli1= $this->getDistance($v['lat'],$v['lng'],$lat2,$lng2);
                $data[$k]['juli']=($juli1/1000);
                $img=!empty($v['thumb'])?explode(',',$v['thumb']):'';
                if ($img!=''){
                    $imgs=array_filter($img);
//                    $v['thum;
                    foreach ($imgs as $g){
                        $v['thumb']=$g;
                        break;
                    }

                }else{
                    $v['thumb']= '';
                }
                $data[$k]['thumb']=$v['thumb'];


            }



            return  $data;
        }



    }


    public  function getDistance($lat1, $lng1, $lat2, $lng2){
        $earthRadius = 6367000; //approximate radius of earth in meters
        $lat1 = ($lat1 * pi() ) / 180;
        $lng1 = ($lng1 * pi() ) / 180;
        $lat2 = ($lat2 * pi() ) / 180;
        $lng2 = ($lng2 * pi() ) / 180;
        $calcLongitude = $lng2 - $lng1;
        $calcLatitude = $lat2 - $lat1;
        $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
        $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
        $calculatedDistance = $earthRadius * $stepTwo;
        return round($calculatedDistance);
    }


}
